using System;
using System.Collections.Generic;
using System.Text;

namespace Algorithms.Numerical.Impl
{
    public class SelectionViewDecorator : Matrix
    {
        private Matrix source;
        private int[] rowIndexes;
        private int[] colIndexes;

        public SelectionViewDecorator(Matrix source, int[] rowIndexes, int[] colIndexes)
        {            
            if (rowIndexes == null)
                rowIndexes = this.range(source.Height);            
            if (colIndexes == null)
                colIndexes = this.range(source.Width);

            this.source = source;
            this.rowIndexes = rowIndexes;
            this.colIndexes = colIndexes;

            for (int i = 0; i < rowIndexes.Length; i++)
                if (rowIndexes[i] < 0 || rowIndexes[i] >= source.Height)
                    throw new ArgumentException();
            for (int i = 0; i < colIndexes.Length; i++)
                if (colIndexes[i] < 0 || colIndexes[i] >= source.Width)
                    throw new ArgumentException();
        }


        public override int Height
        {
            get
            {
                return this.rowIndexes.Length;
            }
        }

        public override int Width
        {
            get
            {
                return this.colIndexes.Length;
            }
        }

        public override double this[int y, int x]
        {
            get
            {
                return this.source[this.rowIndexes[y], this.colIndexes[x]];
            }
            set
            {
                this.source[this.rowIndexes[y], this.colIndexes[x]] = value;
            }
        }

        private int[] range(int limit)
        {
            int[] r = new int[limit];
            for (int i = 0; i < limit; i++)
                r[i] = i;
            return r;
        }

    }
}
